import React, { Fragment } from 'react';
import { Redirect, Switch, Route } from 'dva/router';

import styles from './DataCenter.less';

import { getRoutes, getPageQuery, getQueryPath } from '../utils/utils';
import DocumentTitle from 'react-document-title';

function getLoginPathWithRedirectPath() {
  const params = getPageQuery();
  const { redirect } = params;
  return getQueryPath('/sys/module', {
    redirect,
  });
}

class DataCenterLayout extends React.PureComponent {
  getPageTitle() {
    const { routerData, location } = this.props;
    const { pathname } = location;
    let title = '数据中心';
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - 数据中心`;
    }
    return title;
  }

  render() {
    const { routerData, match } = this.props;
    return (
      <DocumentTitle title={this.getPageTitle()}>
        <div className={styles.containers}>
          <Switch>
            {getRoutes(match.path, routerData).map(item => (
              <Route
                key={item.key}
                path={item.path}
                component={item.component}
                exact={item.exact}
              />
            ))}
            <Redirect from="/sys" to={getLoginPathWithRedirectPath()} />
          </Switch>
        </div>
      </DocumentTitle>
    );
  }
}

export default DataCenterLayout;
